<?php
class Twitter{

	private $username;
	private $password;
	private $owner_username;
	
	public function __construct($username,$password,$owner_username){
		if(!extension_loaded('curl')){
			echo 'Pre-run checks have indicated your PHP installation does not have Curl support.';
			die();
		}
		$this->username			=	$username;
		$this->password			=	$password;
		$this->owner_username	=	$owner_username;
	}

	public function action($action,$postdata = NULL,$postdata2 = NULL){
		$curl_connection = curl_init();
		global $status_text, $status_id, $status_user;
		global $message_text, $message_id, $message_user;
		curl_setopt($curl_connection, CURLOPT_USERPWD, "$this->username:$this->password");
		curl_setopt($curl_connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($curl_connection, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, TRUE);
		
		if($action == 'get_status'){
			$url = 'https://twitter.com/statuses/user_timeline.xml?screen_name='."$this->username";
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			$output = new SimpleXMLElement(curl_exec($curl_connection));
			if($output->error = NULL){
				$status_text = $output->status->text;
				$status_text = explode(' ', $status_text);
				$status_id = $output->status->id;
				$status_user = $output->status->user->screen_name;
			}
		}elseif($action == 'update_status' && $postdata != NULL){
			$url = 'https://twitter.com/statuses/update.xml';
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "status=$postdata");
			curl_setopt($curl_connection, CURLOPT_POST, 1);
			curl_exec($curl_connection);
			
		}elseif($action == 'delete_status'){
			if($postdata != NULL){
				$url = 'https://twitter.com/statuses/destroy/'."$postdata.xml";
			}else{
				$url = 'https://twitter.com/statuses/destroy/'."$status_id.xml";
			}
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			curl_setopt($curl_connection, CURLOPT_CUSTOMREQUEST, 'DELETE');
			curl_exec($curl_connection);
		}elseif($action == 'get_messages'){
			$url = 'http://twitter.com/direct_messages.xml';
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			$output = new SimpleXMLElement(curl_exec($curl_connection));
			$message_text = $output->direct_message->text;
			$message_id = $output->direct_message->id;
			$message_user = $output->direct_message->sender_screen_name;
		}elseif($action == 'new_message'){
			$url = 'http://twitter.com/direct_messages/new.xml';
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "text=$postdata&screen_name=$postdata2");
			curl_setopt($curl_connection, CURLOPT_POST, 1);
			curl_exec($curl_connection);
			echo $message_id;
		}elseif($action == 'delete_message'){
			$url = 'http://twitter.com/direct_messages/destroy/'."$message_id".'.xml';
			echo $url;
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			curl_setopt($curl_connection, CURLOPT_CUSTOMREQUEST, 'DELETE');
			$output = new SimpleXMLElement(curl_exec($curl_connection));
			print_r($output);
		}elseif($action == 'follow_user'){
			$url = 'http://twitter.com/friendships/create/'."$postdata".'.xml?follow=true';
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			curl_setopt($curl_connection, CURLOPT_POST, 1);
			curl_exec($curl_connection);
		}elseif($action == 'get_rate_limit'){
			$url = 'http://twitter.com/account/rate_limit_status.xml';
			curl_setopt($curl_connection, CURLOPT_URL, $url);
			$output = new SimpleXMLElement(curl_exec($curl_connection));
			print_r($output);
			// This setting is unfinished. Upon creating the XML element, when trying to select
			// the 'remaining-hits' or 'hourly-limit' tags, PHP interprets '-hits' or '-limit' to
			// be a built in function or command of some kind. As an example, see the result of
			// doing $remaining_hits = $output->remaining-hits and then print_r()ing the output.
			// For now, this is safe to leave this the way it is, however, ideally it would be fixed.
		}
		curl_close($curl_connection);
	}
}
?>